package com.liuheqiao.sushegaunli;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import com.liuheqiao.sushegaunli.fragment.BusinessFragment;
import com.liuheqiao.sushegaunli.fragment.HomeFragment;
import com.liuheqiao.sushegaunli.fragment.MineFragment;

public class MainActivity extends AppCompatActivity implements MineFragment.OnMineItemClickListener {
    private BottomNavigationView bottomNavigationView;
    private Fragment currentFragment;
    private HomeFragment homeFragment;
    private BusinessFragment businessFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragments();
        setupBottomNavigation();
        
        // 默认显示首页
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, homeFragment)
                    .commit();
            currentFragment = homeFragment;
        }
    }

    private void initFragments() {
        homeFragment = new HomeFragment();
        businessFragment = new BusinessFragment();
        mineFragment = new MineFragment();
        mineFragment.setOnMineItemClickListener(this);
    }

    private void setupBottomNavigation() {
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                switchFragment(homeFragment);
                return true;
            } else if (itemId == R.id.nav_business) {
                switchFragment(businessFragment);
                return true;
            } else if (itemId == R.id.nav_mine) {
                switchFragment(mineFragment);
                return true;
            }
            return false;
        });
    }

    private void switchFragment(Fragment targetFragment) {
        if (currentFragment != targetFragment) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (!targetFragment.isAdded()) {
                if (currentFragment != null) {
                    transaction.hide(currentFragment);
                }
                transaction.add(R.id.fragment_container, targetFragment);
            } else {
                transaction.hide(currentFragment).show(targetFragment);
            }
            transaction.commit();
            currentFragment = targetFragment;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 检查登录状态
        checkLoginStatus();
    }

    private void checkLoginStatus() {
        // 如果未登录，跳转到登录页面
        if (!isLoggedIn()) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private boolean isLoggedIn() {
        // 这里可以从 SharedPreferences 或其他地方检查登录状态
        // 临时返回 true，你需要根据实际情况实现
        return true;
    }

    // 实现 OnMineItemClickListener 接口的方法
    @Override
    public void onUserInfoClick() {
        startActivity(new Intent(this, UserInfoActivity.class));
    }

    @Override
    public void onMyApplicationsClick() {
        // TODO: 跳转到我的申请列表页面
        Toast.makeText(this, "跳转到我的申请页面", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSettingsClick() {
        // TODO: 跳转到设置页面
        Toast.makeText(this, "跳转到设置页面", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHelpClick() {
        // TODO: 跳转到帮助页面
        Toast.makeText(this, "跳转到帮助页面", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAboutClick() {
        // TODO: 跳转到关于我们页面
        Toast.makeText(this, "跳转到关于我们页面", Toast.LENGTH_SHORT).show();
    }
}